<?php
namespace Mobile\Controller;
use Think\Controller;
class MemberController extends AppController {

    function __construct()
    {
        parent::__construct();
        $this->M_address = M('Address');
    }

    /**
     * 地址列表
     */
    public function address_list(){
        $address_id=I('post.id');
        if($address_id){
            $list = $this->M_address->where('user_id = '.$this->uid['user_id'].' AND address_id = '.$address_id)->select();
        }else{
            $list = $this->M_address->where('user_id = '.$this->uid['user_id'])->select();
        }

        $this->re_json($list);
    }
    /**
     * 地址添加与编辑
     */
    public function edit_address(){
        $id = I('post.address_id',0,'intval');
        if($id){
            $info = $this->M_address->where('user_id = '.$this->uid['user_id'].' AND address_id = '.$id)->find();
            if(empty($info))
                $this->re_json(array('error'=>'系统错误！'));
            $data=I('post.');
            $this->M_address->save($data)?$this->re_json(array('errMsg'=>'更新成功')):$this->re_json(array('errMsg'=>'更新失败'));
        }else{
            $this->M_address->full_name = I('post.full_name');
            if(I('post.full_name') =='')
                $this->re_json(array('errMsg'=>'收餐人不能为空'));
            $this->M_address->area = I('post.area');
            if(I('post.area') =='')
                $this->re_json(array('errMsg'=>'送餐地区不能为空'));
            $this->M_address->details = I('post.details');
            if(I('post.details') =='')
                $this->re_json(array('errMsg'=>'送餐详细地址不能为空'));
            $this->M_address->tel = I('post.tel');
            if(I('post.tel') =='')
                $this->re_json(array('errMsg'=>'电话号码不能为空'));
            $this->M_address->user_id = $this->uid['user_id'];
            $this->M_address->add()?$this->re_json(array('errMsg'=>'添加成功')):$this->re_json(array('errMsg'=>'添加失败'));
        }
    }

    /**
     *查询送餐地址
     */
    public function info(){
        $id = I('post.id',0,'intval');
        if($id){
            $info = $this->M_address->where('user_id = ' . $this->uid['user_id'] . ' AND address_id = ' . $id)->find();
            $this->re_json($info);
        }
    }

    /**
     * 地址删除
     */
    public function del_address(){
        $id = I('post.id',0,'intval');
        if(empty($id))
            $this->re_json(array('errMsg'=>'参数不能为空！'));
        $this->M_address->where('user_id = '.$this->uid['user_id'].' AND address_id = '.$id)->delete()?$this->re_json(array('errMsg'=>'删除成功')):$this->re_json(array('errMsg'=>'删除失败'));
    }







}
